using System;
using System.Data;
using System.Configuration;
using Pheonix.Fashionzoomer.DAL;
using System.Data.SqlClient;
using System.Collections;
using Pheonix.CodeLibrary.Data;
using Pheonix.CodeLibrary;

/// <summary>
/// Summary description for Subscribe
/// </summary>

namespace Pheonix.Fashionzoomer.Entity.SqlServer2000
{
    [System.ComponentModel.DataObject]
    public class Subscribe : SqlServerDAO
    {
        #region Subscribe Private Fields

		private Int32 _id;
		private String _email;

        #endregion

        #region Subscribe Constractors

		public Subscribe(){}

		public Subscribe
		(
			Int32 id,
			String email

		)
		{
			this._id = id;
			this._email = email;
		}

        #endregion

        #region Subscribe Properties

		public Int32 Id
		{
			get{ return this._id; }
			set{ this._id = value; }
		}

		public String Email
		{
			get{ return this._email; }
			set{ this._email = value; }
		}


        #endregion

        #region Subscribe Static Methods

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
        public static Int32 CreateNewSubscribe
        (
			String email

        )
        {
            //creating an empty Subscribe object
            Subscribe subscribe = new Subscribe();
            subscribe.Email = email;

            return Subscribe.CreateNewSubscribe(subscribe);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public static bool UpdateSubscribe
        (
			Int32 id,
			String email

        )
        {
            Subscribe subscribe = Subscribe.GetSubscribeById(id);
            subscribe.Email = email;

            return Subscribe.UpdateSubscribe(subscribe);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public static bool DeleteSubscribe
        (
            Int32 id
        )
        {
            //Validate Input
            if (id <= GetIdMinValue)
                throw (new ArgumentOutOfRangeException("id in Subscribe"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddParameterToSqlCmd(sqlCmd, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, id);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_SUBSCRIBE_DELETESUBSCRIBE);
            GetOneData(sqlCmd);
            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return (returnValue == 0 ? false : true);

        }

        public static bool DeleteSubscribeList(System.Collections.Generic.List<Int32> idListToDelete)
        {
            //Validate Input
            foreach (Int32 id in idListToDelete)
                if (id <= GetIdMinValue)
                    throw (new ArgumentOutOfRangeException("id in Subscribe"));

            //performing deletion operation //

            string csvString = Utility.MakeCSV(idListToDelete);

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddParameterToSqlCmd(sqlCmd, "@IdList", SqlDbType.NVarChar, csvString.Length, 
                ParameterDirection.Input, csvString);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_SUBSCRIBE_DELETESUBSCRIBELIST);
            GetOneData(sqlCmd);
            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return (returnValue == idListToDelete.Count ? true : false);
        }
		
        #endregion

        #region Subscribe Instance Methods
        
        public bool Save()
		{
			if(Id <= GetIdMinValue)
			{
				Int32 TempId=CreateNewSubscribe(this);
				if(TempId>0)
				{
					_id=TempId;
					return true;
				}
				else
				{
					// error occurs
					return false;
				}
			}
			else
			{	//This is a update Command
				return (UpdateSubscribe(this));
			}
		}

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static CustomCollection<Subscribe> GetAllSubscribe()
        {
            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_SUBSCRIBE_GETALLSUBSCRIBE);
            GenerateCollectionFromReader result = new GenerateCollectionFromReader(GenerateSubscribeCollectionFromReader);
            CustomCollection<Subscribe> objCollection = ((CustomCollection<Subscribe>)GetAllData(sqlCmd, result));

            return objCollection;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static CustomCollection<Subscribe> GetAllSubscribePaged(String orderBy, Int32 startRowIndex, Int32 maximumRows)
        {
            //Validate Input
            if (orderBy == "")
                orderBy = "Id";

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@orderby", SqlDbType.VarChar, 50, ParameterDirection.Input, orderBy);
            AddParameterToSqlCmd(sqlCmd, "@startRowIndex", SqlDbType.Int, 0, ParameterDirection.Input, startRowIndex);
            AddParameterToSqlCmd(sqlCmd, "@maximumRows", SqlDbType.Int, 0, ParameterDirection.Input, maximumRows);

            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_SUBSCRIBE_GETALLSUBSCRIBEPAGED);

            GenerateCollectionFromReader result = new GenerateCollectionFromReader(GenerateSubscribeCollectionFromReader);
            CustomCollection<Subscribe> objCollection = ((CustomCollection<Subscribe>)GetAllData(sqlCmd, result));
            return objCollection;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static int GetAllSubscribePagedCount(String orderBy, Int32 startRowIndex, Int32 maximumRows)
        {
            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();

            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_SUBSCRIBE_GETALLSUBSCRIBEPAGEDCOUNT);
            GetOneData(sqlCmd);

            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return returnValue;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static Subscribe GetSubscribeById(Int32 id)
        {
            if (id <= GetIdMinValue)
                throw (new ArgumentOutOfRangeException("id in Subscribe"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, id);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_SUBSCRIBE_GETSUBSCRIBEBYID);
            GenerateCollectionFromReader result = new GenerateCollectionFromReader(GenerateSubscribeCollectionFromReader);
            CustomCollection<Subscribe> objCollection = ((CustomCollection<Subscribe>)GetAllData(sqlCmd, result));

            if (objCollection.Count > 0)
                return (Subscribe)objCollection[0];
            else
                return null;
        }

        public static Int32 CreateNewSubscribe(Subscribe newSubscribe)
        {
            // validate Parameters
            if (newSubscribe == null)
                throw (new ArgumentNullException("newSubscribe in Subscribe"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);

            AddParameterToSqlCmd(sqlCmd, "@Email", SqlDbType.NVarChar, 64, ParameterDirection.Input, newSubscribe.Email);

            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_SUBSCRIBE_CREATENEWSUBSCRIBE);
            GetOneData(sqlCmd);
            return ((Int32)sqlCmd.Parameters["@ReturnValue"].Value);

        }

        public static bool UpdateSubscribe(Subscribe newSubscribe)
        {
            // validate Parameters
            if (newSubscribe == null)
                throw (new ArgumentNullException("newSubscribe"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);

            AddParameterToSqlCmd(sqlCmd, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, newSubscribe.Id);
            AddParameterToSqlCmd(sqlCmd, "@Email", SqlDbType.NVarChar, 64, ParameterDirection.Input, newSubscribe.Email);

            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_SUBSCRIBE_UPDATESUBSCRIBE);
            GetOneData(sqlCmd);
            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return (returnValue == 0 ? false : true);
        }

        #endregion

        #region Subscribe Related Collections
        /// <summary>
        /// Creates and returns a strongly typed collection of Subscribe custom entity. 
        /// The colelction is created through iterating on the IdataReader object which contains 
        /// Subscribe information, as a set of records, similar to tabular format.
        /// </summary>
        public static IList GenerateSubscribeCollectionFromReader(IDataReader returnData)
        {

            //creating the instance of Subscribe collection
            CustomCollection<Subscribe> colSubscribe = new CustomCollection<Subscribe>();

            //Iterating thriugh the data reader, to generate Subscribe collection.

            //getting the Subscribe collection by iterating thru the data reader
            while (returnData.Read())
            {
                //passing the Subscribe constructor parameters from the current instance of data reader fields.
                Subscribe newSubscribe = new Subscribe
                (
                    returnData["id"] == System.DBNull.Value ? GetIdMinValue : (Int32)returnData["id"],
                    returnData["email"] == System.DBNull.Value ? String.Empty : (String)returnData["email"]

                );
                //adding the Subscribe to the collection
                colSubscribe.Add(newSubscribe);
            }

            //returns the collection of Subscribe 
            return (colSubscribe);
        }

        #endregion

        #region Constants and Default Values

        public static Int32 GetIdMinValue
        {
            get
            {
                return 0;
            }
        }

        //contants
        internal const string SPR_SUBSCRIBE_CREATENEWSUBSCRIBE = "spr_Subscribe_CreateNewSubscribe";
        internal const string SPR_SUBSCRIBE_UPDATESUBSCRIBE = "spr_Subscribe_UpdateSubscribe";
        internal const string SPR_SUBSCRIBE_DELETESUBSCRIBE = "spr_Subscribe_DeleteSubscribe";
        internal const string SPR_SUBSCRIBE_DELETESUBSCRIBELIST = "spr_Subscribe_DeleteSubscribeList";
        internal const string SPR_SUBSCRIBE_GETSUBSCRIBEBYID = "spr_Subscribe_GetSubscribeById";
        internal const string SPR_SUBSCRIBE_GETALLSUBSCRIBE = "spr_Subscribe_GetAllSubscribe";
        internal const string SPR_SUBSCRIBE_GETALLSUBSCRIBEPAGED = "spr_Subscribe_GetAllSubscribePaged";
        internal const string SPR_SUBSCRIBE_GETALLSUBSCRIBEPAGEDCOUNT = "spr_Subscribe_GetAllSubscribePagedCount";

		//Foreign key contants
		
        #endregion
    }
}
